﻿<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->

<UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
  xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls.Toolkit"
  x:Class="System.Windows.Controls.Samples.TreeViewSelectionSample">
    <StackPanel>
        <!-- TreeView and TreeViewItem Events -->
        <ContentControl Content="SelectedItem and SelectedValue" Style="{StaticResource Header}" />
        <StackPanel>
            <TextBlock Text="TreeView.SelectedItem" Style="{StaticResource ApiName}" />
            <TextBlock Style="{StaticResource ApiDescription}">
                The selected object in the TreeView, or null if no item is
                selected.
            </TextBlock>

            <TextBlock Text="TreeView.SelectedValuePath" Style="{StaticResource ApiName}" />
            <TextBlock Style="{StaticResource ApiDescription}">
                A string that contains the path that is used to get the
                SelectedValue.
            </TextBlock>

            <TextBlock Text="TreeView.SelectedValue" Style="{StaticResource ApiName}" />
            <TextBlock Style="{StaticResource ApiDescription}">
                Gets the object that is at the specified SelectedValuePath of
                the SelectedItem, or null if no item is selected.
            </TextBlock>
        </StackPanel>

        <!-- Selection example -->
        <ContentControl Content="Selection" Style="{StaticResource Header}" />
        <TextBlock Style="{StaticResource Information}">
            Change the selection to see the SelectedItem and SelectedValue.
        </TextBlock>
        <toolkit:TreeView
          x:Name="TreeOfLife"
          SelectedItemChanged="OnSelectedItemChanged"
          SelectedValuePath="Classification"
          Margin="5">
            <toolkit:TreeView.ItemTemplate>
                <common:HierarchicalDataTemplate ItemsSource="{Binding Subclasses}">
                    <StackPanel>
                        <TextBlock Text="{Binding Rank}" FontSize="8" FontStyle="Italic" Foreground="Gray" Margin="0 0 0 -5" />
                        <TextBlock Text="{Binding Classification}" />
                    </StackPanel>
                </common:HierarchicalDataTemplate>
            </toolkit:TreeView.ItemTemplate>
        </toolkit:TreeView>
        <Grid HorizontalAlignment="Left">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <TextBlock Text="SelectedItem:" Style="{StaticResource Information}" VerticalAlignment="Center" />
            <ContentControl x:Name="SelectedItem" FontWeight="Bold" Grid.Column="1" VerticalAlignment="Center" />
            <TextBlock Grid.Row="1" Text="SelectedValue:" Style="{StaticResource Information}" VerticalAlignment="Center" />
            <ContentControl x:Name="SelectedValue" FontWeight="Bold" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" />
        </Grid>

        <src:SourceViewer xmlns:src="clr-namespace:System.Windows.Controls.Samples;assembly=System.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <src:SourceFile Path="TreeViewSelectionSample.xaml">
    <src:SourceFile.Source>
      <sys:String>&lt;!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
--&gt;

&lt;UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
  xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls.Toolkit"
  x:Class="System.Windows.Controls.Samples.TreeViewSelectionSample"&gt;
    &lt;StackPanel&gt;
        &lt;!-- TreeView and TreeViewItem Events --&gt;
        &lt;ContentControl Content="SelectedItem and SelectedValue" Style="{StaticResource Header}" /&gt;
        &lt;StackPanel&gt;
            &lt;TextBlock Text="TreeView.SelectedItem" Style="{StaticResource ApiName}" /&gt;
            &lt;TextBlock Style="{StaticResource ApiDescription}"&gt;
                The selected object in the TreeView, or null if no item is
                selected.
            &lt;/TextBlock&gt;

            &lt;TextBlock Text="TreeView.SelectedValuePath" Style="{StaticResource ApiName}" /&gt;
            &lt;TextBlock Style="{StaticResource ApiDescription}"&gt;
                A string that contains the path that is used to get the
                SelectedValue.
            &lt;/TextBlock&gt;

            &lt;TextBlock Text="TreeView.SelectedValue" Style="{StaticResource ApiName}" /&gt;
            &lt;TextBlock Style="{StaticResource ApiDescription}"&gt;
                Gets the object that is at the specified SelectedValuePath of
                the SelectedItem, or null if no item is selected.
            &lt;/TextBlock&gt;
        &lt;/StackPanel&gt;

        &lt;!-- Selection example --&gt;
        &lt;ContentControl Content="Selection" Style="{StaticResource Header}" /&gt;
        &lt;TextBlock Style="{StaticResource Information}"&gt;
            Change the selection to see the SelectedItem and SelectedValue.
        &lt;/TextBlock&gt;
        &lt;toolkit:TreeView
          x:Name="TreeOfLife"
          SelectedItemChanged="OnSelectedItemChanged"
          SelectedValuePath="Classification"
          Margin="5"&gt;
            &lt;toolkit:TreeView.ItemTemplate&gt;
                &lt;common:HierarchicalDataTemplate ItemsSource="{Binding Subclasses}"&gt;
                    &lt;StackPanel&gt;
                        &lt;TextBlock Text="{Binding Rank}" FontSize="8" FontStyle="Italic" Foreground="Gray" Margin="0 0 0 -5" /&gt;
                        &lt;TextBlock Text="{Binding Classification}" /&gt;
                    &lt;/StackPanel&gt;
                &lt;/common:HierarchicalDataTemplate&gt;
            &lt;/toolkit:TreeView.ItemTemplate&gt;
        &lt;/toolkit:TreeView&gt;
        &lt;Grid HorizontalAlignment="Left"&gt;
            &lt;Grid.RowDefinitions&gt;
                &lt;RowDefinition /&gt;
                &lt;RowDefinition /&gt;
            &lt;/Grid.RowDefinitions&gt;
            &lt;Grid.ColumnDefinitions&gt;
                &lt;ColumnDefinition /&gt;
                &lt;ColumnDefinition /&gt;
            &lt;/Grid.ColumnDefinitions&gt;
            &lt;TextBlock Text="SelectedItem:" Style="{StaticResource Information}" VerticalAlignment="Center" /&gt;
            &lt;ContentControl x:Name="SelectedItem" FontWeight="Bold" Grid.Column="1" VerticalAlignment="Center" /&gt;
            &lt;TextBlock Grid.Row="1" Text="SelectedValue:" Style="{StaticResource Information}" VerticalAlignment="Center" /&gt;
            &lt;ContentControl x:Name="SelectedValue" FontWeight="Bold" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" /&gt;
        &lt;/Grid&gt;
    &lt;/StackPanel&gt;
&lt;/UserControl&gt;</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="TreeViewSelectionSample.xaml.cs">
    <src:SourceFile.Source>
      <sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace System.Windows.Controls.Samples
{
    /// &lt;summary&gt;
    /// Sample demonstrating TreeView selection.
    /// &lt;/summary&gt;
    [Sample("(2)Selection", DifficultyLevel.Basic)]
    [Category("TreeView")]
    public partial class TreeViewSelectionSample : UserControl
    {
        /// &lt;summary&gt;
        /// Initializes a new instance of the TreeViewSelectionSample class.
        /// &lt;/summary&gt;
        public TreeViewSelectionSample()
        {
            InitializeComponent();

            // Fill the tree with data
            TreeOfLife.ItemsSource = Taxonomy.Life;
        }

        /// &lt;summary&gt;
        /// Handle the TreeView.SelectedItemChanged event.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The TreeView.&lt;/param&gt;
        /// &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "The event handler is declared in XAML.")]
        private void OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs&lt;object&gt; e)
        {
            TreeView view = sender as TreeView;
            SelectedItem.Content = view.SelectedItem;
            SelectedValue.Content = view.SelectedValue;
        }
    }
}</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="TreeViewSelectionSample.xaml.vb">
    <src:SourceFile.Source>
      <sys:String>' (c) Copyright Microsoft Corporation.
' This source is subject to the Microsoft Public License (Ms-PL).
' Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
' All other rights reserved.

Imports Microsoft.VisualBasic
Imports System
Imports System.Diagnostics.CodeAnalysis
Imports System.Globalization
Imports System.Windows
Imports System.Windows.Controls
Imports System.ComponentModel

''' &lt;summary&gt;
''' Sample demonstrating TreeView selection.
''' &lt;/summary&gt;
&lt;Sample("(2)Selection", DifficultyLevel.Basic), Category("TreeView")&gt; _
Partial Public Class TreeViewSelectionSample
    Inherits UserControl
    ''' &lt;summary&gt;
    ''' Initializes a new instance of the TreeViewSelectionSample class.
    ''' &lt;/summary&gt;
    &lt;System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors", Justification:="InitializeComponent Required by Visual Studio.")&gt; _
    Public Sub New()
        InitializeComponent()

        ' Fill the tree with data
        TreeOfLife.ItemsSource = Taxonomy.Life
    End Sub

    ''' &lt;summary&gt;
    ''' Handle the TreeView.SelectedItemChanged event.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;The TreeView.&lt;/param&gt;
    ''' &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
    &lt;SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification:="The event handler is declared in XAML.")&gt; _
    Private Sub OnSelectedItemChanged(ByVal sender As Object, ByVal e As RoutedPropertyChangedEventArgs(Of Object))
        Dim view As TreeView = TryCast(sender, TreeView)
        SelectedItem.Content = view.SelectedItem
        SelectedValue.Content = view.SelectedValue
    End Sub
End Class
</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
</src:SourceViewer>
    </StackPanel>
</UserControl>